//
//  MoreMovieViewController.m
//  美食百科
//
//  Created by dllo on 16/2/3.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "MoreMovieViewController.h"
#import "FoodViewController.h"
#import "MovieTableViewCell.h"
#import "MJRefresh.h"
#import "NewWorking.h"
#import "Food.h"
#import "Reachability.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface MoreMovieViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain) UITableView *tableView;

@property(nonatomic, assign) NSInteger page;

@end

@implementation MoreMovieViewController
-(void)dealloc
{
    [_array release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        
        //如果无网络,弹窗提示
        if ( 0 == reach.currentReachabilityStatus){
            //弹窗提示
            UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
            nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
            nonconnectionLabel.backgroundColor = [UIColor redColor];
            nonconnectionLabel.text = @"网络不给力,刷新试试~";
            nonconnectionLabel.textColor = [UIColor whiteColor];
            [self.view addSubview:nonconnectionLabel];
            
            //动画效果,通知栏缓慢出现
            [UIView animateWithDuration:0.6 animations:^{
                nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                    nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                    [nonconnectionLabel release];
                    
                } completion:^(BOOL finished) {
                    
                }];
                
                //            [UIView animateWithDuration:1.9 animations:^{
                //                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                //            //[nonconnectionLabel removeFromSuperview];
                //                [nonconnectionLabel release];
                //            }];
                
            }];
            
        }
    });

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64) style:UITableViewStylePlain];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.separatorStyle = NO;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.page = 1;
    
[self.tableView addFooterWithCallback:^{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        
        //如果无网络,弹窗提示
        if ( 0 == reach.currentReachabilityStatus){
            //弹窗提示
            UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
            nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
            nonconnectionLabel.backgroundColor = [UIColor redColor];
            nonconnectionLabel.text = @"网络不给力,刷新试试~";
            nonconnectionLabel.textColor = [UIColor whiteColor];
            [self.view addSubview:nonconnectionLabel];
            
            //动画效果,通知栏缓慢出现
            [UIView animateWithDuration:0.6 animations:^{
                nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                    nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                    [nonconnectionLabel release];
                    
                } completion:^(BOOL finished) {
                    
                }];
                
                //            [UIView animateWithDuration:1.9 animations:^{
                //                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                //            //[nonconnectionLabel removeFromSuperview];
                //                [nonconnectionLabel release];
                //            }];
                
            }];
            
        }
    });

    
    ++self.page;
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SceneDishes",@"methodName",[NSNumber numberWithInteger:20],@"size",[NSNumber numberWithInteger:self.page++],@"page",[NSNumber numberWithInteger:51],@"scene_id",[NSNumber numberWithFloat:4.3],@"version",nil];
    
    [NewWorking PostDayaWithURL:@"http://api.izhangchu.com/" dic:dictionary sussess:^(id responseObject) {
        for (NSDictionary *dic in responseObject[@"data"][@"data"]) {
            Food *food = [[Food alloc] init];
            [food setValuesForKeysWithDictionary:dic];
            [self.array addObject:food];
            [food release];
        }
        
        [self.tableView reloadData];
        
        [self.tableView footerEndRefreshing];

    } filed:^(NSError *error) {
        NSLog(@"%@",error);
        [self.tableView footerEndRefreshing];

    }];
}];
    self.tableView.footerPullToRefreshText = @"上拉加载数据~";
    
    //上拉到一定程度后加载显示的内容
    self.tableView.footerReleaseToRefreshText = @"松开加载数据~";
    
    //上拉松开后加载显示的内容
    self.tableView.footerRefreshingText = @"数据加载中~";
    [self.tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"moreMovie"];
    
    [self.view addSubview:self.tableView];
    [_tableView release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
        
}
    
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreMovie"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.food = self.array[indexPath.row];
        
    return cell;
        
}
    
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kHeight-kHeight/5*2)/3.5;
}
    
//跳转介绍界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodViewController *foodVC = [[FoodViewController alloc] init];
        
    foodVC.food = self.array[indexPath.row];
        
    [self.navigationController pushViewController:foodVC animated:YES];
    
    [foodVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
